package cn.ibizlab.assetmanagement.assetapi.rest;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.math.BigInteger;
import java.util.HashMap;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.ServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import cn.ibizlab.assetmanagement.assetapi.dto.*;
import cn.ibizlab.assetmanagement.assetapi.mapping.*;
import cn.ibizlab.assetmanagement.core.asset.domain.EAMDocumentType;
import cn.ibizlab.assetmanagement.core.asset.service.IEAMDocumentTypeService;
import cn.ibizlab.assetmanagement.core.asset.filter.EAMDocumentTypeSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"文档类型" })
@RestController("AssetApi-eamdocumenttype")
@RequestMapping("")
public class EAMDocumentTypeResource {

    @Autowired
    public IEAMDocumentTypeService eamdocumenttypeService;

    @Autowired
    @Lazy
    public EAMDocumentTypeMapping eamdocumenttypeMapping;

    @PreAuthorize("hasPermission(this.eamdocumenttypeMapping.toDomain(#eamdocumenttypedto),'iBizAssetManagement-EAMDocumentType-Create')")
    @ApiOperation(value = "新建文档类型", tags = {"文档类型" },  notes = "新建文档类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamdocumenttypes")
    @Transactional
    public ResponseEntity<EAMDocumentTypeDTO> create(@RequestBody EAMDocumentTypeDTO eamdocumenttypedto) {
        EAMDocumentType domain = eamdocumenttypeMapping.toDomain(eamdocumenttypedto);
		eamdocumenttypeService.create(domain);
        EAMDocumentTypeDTO dto = eamdocumenttypeMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamdocumenttypeMapping.toDomain(#eamdocumenttypedtos),'iBizAssetManagement-EAMDocumentType-Create')")
    @ApiOperation(value = "批量新建文档类型", tags = {"文档类型" },  notes = "批量新建文档类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamdocumenttypes/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMDocumentTypeDTO> eamdocumenttypedtos) {
        eamdocumenttypeService.createBatch(eamdocumenttypeMapping.toDomain(eamdocumenttypedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamdocumenttype" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamdocumenttypeService.get(#eamdocumenttype_id),'iBizAssetManagement-EAMDocumentType-Update')")
    @ApiOperation(value = "更新文档类型", tags = {"文档类型" },  notes = "更新文档类型")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamdocumenttypes/{eamdocumenttype_id}")
    @Transactional
    public ResponseEntity<EAMDocumentTypeDTO> update(@PathVariable("eamdocumenttype_id") String eamdocumenttype_id, @RequestBody EAMDocumentTypeDTO eamdocumenttypedto) {
		EAMDocumentType domain  = eamdocumenttypeMapping.toDomain(eamdocumenttypedto);
        domain .setEamdocumenttypeid(eamdocumenttype_id);
		eamdocumenttypeService.update(domain );
		EAMDocumentTypeDTO dto = eamdocumenttypeMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamdocumenttypeService.getEamdocumenttypeByEntities(this.eamdocumenttypeMapping.toDomain(#eamdocumenttypedtos)),'iBizAssetManagement-EAMDocumentType-Update')")
    @ApiOperation(value = "批量更新文档类型", tags = {"文档类型" },  notes = "批量更新文档类型")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamdocumenttypes/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMDocumentTypeDTO> eamdocumenttypedtos) {
        eamdocumenttypeService.updateBatch(eamdocumenttypeMapping.toDomain(eamdocumenttypedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.eamdocumenttypeService.get(#eamdocumenttype_id),'iBizAssetManagement-EAMDocumentType-Remove')")
    @ApiOperation(value = "删除文档类型", tags = {"文档类型" },  notes = "删除文档类型")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamdocumenttypes/{eamdocumenttype_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("eamdocumenttype_id") String eamdocumenttype_id) {
         return ResponseEntity.status(HttpStatus.OK).body(eamdocumenttypeService.remove(eamdocumenttype_id));
    }

    @PreAuthorize("hasPermission(this.eamdocumenttypeService.getEamdocumenttypeByIds(#ids),'iBizAssetManagement-EAMDocumentType-Remove')")
    @ApiOperation(value = "批量删除文档类型", tags = {"文档类型" },  notes = "批量删除文档类型")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamdocumenttypes/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        eamdocumenttypeService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.eamdocumenttypeMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMDocumentType-Get')")
    @ApiOperation(value = "获取文档类型", tags = {"文档类型" },  notes = "获取文档类型")
	@RequestMapping(method = RequestMethod.GET, value = "/eamdocumenttypes/{eamdocumenttype_id}")
    public ResponseEntity<EAMDocumentTypeDTO> get(@PathVariable("eamdocumenttype_id") String eamdocumenttype_id) {
        EAMDocumentType domain = eamdocumenttypeService.get(eamdocumenttype_id);
        EAMDocumentTypeDTO dto = eamdocumenttypeMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取文档类型草稿", tags = {"文档类型" },  notes = "获取文档类型草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamdocumenttypes/getdraft")
    public ResponseEntity<EAMDocumentTypeDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamdocumenttypeMapping.toDto(eamdocumenttypeService.getDraft(new EAMDocumentType())));
    }

    @ApiOperation(value = "检查文档类型", tags = {"文档类型" },  notes = "检查文档类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamdocumenttypes/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMDocumentTypeDTO eamdocumenttypedto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamdocumenttypeService.checkKey(eamdocumenttypeMapping.toDomain(eamdocumenttypedto)));
    }

    @PreAuthorize("hasPermission(this.eamdocumenttypeMapping.toDomain(#eamdocumenttypedto),'iBizAssetManagement-EAMDocumentType-Save')")
    @ApiOperation(value = "保存文档类型", tags = {"文档类型" },  notes = "保存文档类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamdocumenttypes/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMDocumentTypeDTO eamdocumenttypedto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamdocumenttypeService.save(eamdocumenttypeMapping.toDomain(eamdocumenttypedto)));
    }

    @PreAuthorize("hasPermission(this.eamdocumenttypeMapping.toDomain(#eamdocumenttypedtos),'iBizAssetManagement-EAMDocumentType-Save')")
    @ApiOperation(value = "批量保存文档类型", tags = {"文档类型" },  notes = "批量保存文档类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamdocumenttypes/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMDocumentTypeDTO> eamdocumenttypedtos) {
        eamdocumenttypeService.saveBatch(eamdocumenttypeMapping.toDomain(eamdocumenttypedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMDocumentType-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMDocumentType-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"文档类型" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/eamdocumenttypes/fetchdefault")
	public ResponseEntity<List<EAMDocumentTypeDTO>> fetchDefault(EAMDocumentTypeSearchContext context) {
        Page<EAMDocumentType> domains = eamdocumenttypeService.searchDefault(context) ;
        List<EAMDocumentTypeDTO> list = eamdocumenttypeMapping.toDto(domains.getContent());
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
                .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
                .header("x-total", String.valueOf(domains.getTotalElements()))
                .body(list);
	}

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMDocumentType-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMDocumentType-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"文档类型" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/eamdocumenttypes/searchdefault")
	public ResponseEntity<Page<EAMDocumentTypeDTO>> searchDefault(@RequestBody EAMDocumentTypeSearchContext context) {
        Page<EAMDocumentType> domains = eamdocumenttypeService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(eamdocumenttypeMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMDocumentType-searchTop-all') and hasPermission(#context,'iBizAssetManagement-EAMDocumentType-Get')")
	@ApiOperation(value = "获取顶级文档类型", tags = {"文档类型" } ,notes = "获取顶级文档类型")
    @RequestMapping(method= RequestMethod.GET , value="/eamdocumenttypes/fetchtop")
	public ResponseEntity<List<EAMDocumentTypeDTO>> fetchTop(EAMDocumentTypeSearchContext context) {
        Page<EAMDocumentType> domains = eamdocumenttypeService.searchTop(context) ;
        List<EAMDocumentTypeDTO> list = eamdocumenttypeMapping.toDto(domains.getContent());
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
                .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
                .header("x-total", String.valueOf(domains.getTotalElements()))
                .body(list);
	}

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMDocumentType-searchTop-all') and hasPermission(#context,'iBizAssetManagement-EAMDocumentType-Get')")
	@ApiOperation(value = "查询顶级文档类型", tags = {"文档类型" } ,notes = "查询顶级文档类型")
    @RequestMapping(method= RequestMethod.POST , value="/eamdocumenttypes/searchtop")
	public ResponseEntity<Page<EAMDocumentTypeDTO>> searchTop(@RequestBody EAMDocumentTypeSearchContext context) {
        Page<EAMDocumentType> domains = eamdocumenttypeService.searchTop(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(eamdocumenttypeMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

